﻿using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MVVM_CAD.Common;
using MVVM_CAD.Strategy;
using Ninject;

namespace MVVM_CAD.ViewModel
{
    public class ToolsViewModel : ViewModelBase
    {
        #region Members
        private readonly IKernel _kernel;
        #region Strategy Property
        /// <summary>
        /// The <see cref="Strategy" /> property's name.
        /// </summary>
        public const string StrategyPropertyName = "Strategy";

        private IStrategy _strategy = null;

        public IStrategy Strategy
        {
            get
            {
                return _strategy;
            }

            set
            {
                if (_strategy == value)
                {
                    return;
                }

                var oldValue = _strategy;
                _strategy = value;

                Messenger.Default.Send(value);
                RaisePropertyChanged(StrategyPropertyName);
            }
        }
        #endregion

        #endregion

        #region Commands
        #region LineToolCommand
        private ICommand _lineToolCommand = null;
        public ICommand LineToolCommand
        {
            get
            {
                return _lineToolCommand ??
                       (_lineToolCommand = new RelayCommand(() => Strategy = _kernel.Get<LineToolStrategy>()));
            }
        }
        #endregion
        #region PointToolCommand
        private ICommand _pointToolCommand = null;
        public ICommand PointToolCommand
        {
            get
            {
                return _pointToolCommand ??
                       (_pointToolCommand = new RelayCommand(() => Strategy = _kernel.Get<PointToolStrategy>()));
            }
        }
        #endregion
        #region PolyLineToolCommand
        private ICommand _polyLineToolCommand = null;
        public ICommand PolyLineToolCommand
        {
            get
            {
                return _polyLineToolCommand ??
                       (_polyLineToolCommand = new RelayCommand(() => Strategy = _kernel.Get<PolyLineToolStrategy>()));
            }
        }
        #endregion
        #region SelectionToolCommand
        private ICommand _selectionToolCommand = null;
        public ICommand SelectionToolCommand
        {
            get
            {
                return _selectionToolCommand ??
                       (_selectionToolCommand = new RelayCommand(() => Strategy = _kernel.Get<SelectionToolStrategy>()));
            }
        }
        #endregion
        #region CircleToolCommand
        private ICommand _circleToolCommand = null;
        public ICommand CircleToolCommand
        {
            get
            {
                return _circleToolCommand ??
                       (_circleToolCommand = new RelayCommand(() => Strategy = _kernel.Get<CircleToolStrategy>()));
            }
        }
        #endregion
        #region ArcToolCommand
        private ICommand _arcToolCommand = null;

        public ICommand ArcToolCommand
        {
            get
            {
                return _arcToolCommand ??
                       (_arcToolCommand = new RelayCommand(() => Strategy = _kernel.Get<ArcToolStrategy>()));
            }
        }

        #endregion
        #region PolygonToolCommand
        private ICommand _polygonToolCommand = null;

        public ICommand PolygonToolCommand
        {
            get
            {
                return _polygonToolCommand ??
                       (_arcToolCommand = new RelayCommand(() => Strategy = _kernel.Get<PolygonToolStrategy>()));
            }
        }

        #endregion
        #region SplineToolCommand

        private ICommand _splineToolCommand = null;
        public ICommand SplineToolCommand
        {
            get
            {
                return _splineToolCommand ??
                       (_splineToolCommand = new RelayCommand(() => Strategy = _kernel.Get<SplineToolStrategy>()));
            }
        }
        #endregion
        #region DeleteToolCommand

        private ICommand _deleteToolCommand = null;

        public ICommand DeleteToolCommand
        {
            get
            {
                return _deleteToolCommand ??
                       (_deleteToolCommand = new RelayCommand(() => Strategy = _kernel.Get<DeleteToolStrategy>()));
            }
        }
        #endregion

        #endregion

        public ToolsViewModel(IStrategy defaultStrategy)
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                _kernel = new StandardKernel(new MainViewModelModule());
                _strategy = defaultStrategy;
            }
        }
    }
}